<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.io.IOException"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.log4j.Logger"%>
<html>
<head>
<title>测试JSP分页</title>
<script type="text/javascript">
			function changePage(cup){
				document.mypage.cp.value = cup;
				//alert(" === "+cup);
				document.mypage.submit();
			}
			
			function changeSelect(){
				document.mypage.cp.value = document.mypage.mySelect.value;
				//alert(" --- "+document.mypage.mySelect.value);
				document.mypage.submit();
			}
		</script>
</head>

<%
	// 1 表示当前页
	int currentPage = 1;
	// 2 表示每页要显示的记录数
	int lineSize = 6;
	// 3 表示总记录数
	int allRecords;
	// 4 表示总的页数
	int pageSize;

	Logger log = Logger.getLogger(this.getClass());
	String sql = "select * from course;";
	String url = "jdbc:mysql://192.168.7.141:3306/eschool";
	List r = new ArrayList();
	Connection con = null;
	ResultSet rs = null;
	try {
		con = DriverManager.getConnection(url, "root", null);
		rs = con.createStatement(
				java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
				java.sql.ResultSet.CONCUR_READ_ONLY).executeQuery(sql);
		while (rs.next()) {
			Map m = new HashMap();
			m.put("name", rs.getString("name"));
			m.put("lecturer", rs.getString("lecturer"));
			m.put("period", rs.getString("period"));
			m.put("vedio_url", rs.getString("video_url"));
			r.add(m);
		}
	} catch (SQLException se) {
		log.error("", se);
	} finally {
		try {
			rs.close();
			con.close();
		} catch (Exception inr) {
			log.error("", inr);
		}
	}
	request.setAttribute("r", r);

	// 得到总的记录数
	allRecords = r.size();

	// 得到总的页数
	if (allRecords % lineSize == 0) {
		pageSize = allRecords / lineSize;
	} else {
		pageSize = allRecords / lineSize + 1;
	}

	// 处理当前页的变化的情况
	try {
		String hidden = request.getParameter("cp");
		if (hidden == null) {
			hidden = 1 + "";
		}
		currentPage = Integer.parseInt(hidden);
	} catch (Exception ex) {

	}

	// 根据当前页的变化取得不同的数据
	List prs = new ArrayList();
	for (int i = (currentPage - 1) * lineSize; i < currentPage
			* lineSize; i++) {
		prs.add(r.get(i));
	}
%>

<body>
	<h3>JSP分页</h3>
	<br>
	<form name="mypage" action="page.jsp" method="post">

		<input type="button" name="first" value="第一页" onClick="changePage(1)"
			<%=currentPage == 1 ? "disabled" : " "%> /> <input type="button"
			name="pre" value="上一页" onClick="changePage(<%=currentPage - 1%>)"
			<%=currentPage == 1 ? "disabled" : " "%> /> <input type="button"
			name="next" value="下一页" onClick="changePage(<%=currentPage + 1%>)"
			<%=currentPage == pageSize ? "disabled" : " "%> /> <input
			type="button" name="last" value="最后页"
			onClick="changePage(<%=pageSize%>)"
			<%=currentPage == pageSize ? "disabled" : " "%> /> <input
			type="hidden" name="cp" /> <font size="4" color="red"><%=currentPage%>
		</font> / <font size="4" color="red"><%=pageSize%> </font> 跳转到 <select
			name="mySelect" onChange="changeSelect()">
			<%
				for (int i = 1; i <= pageSize; i++) {
			%>
			<option value="<%=i%>" <%=currentPage == i ? "selected" : " "%>>
				<%=i%>
			</option>
			<%
				}
			%>
		</select> 页

	</form>

	<hr width="68%">
	<br>

	<table border="1">
		<tr>
			<td>课程</td>
			<td>讲师</td>
			<td>课时</td>
		</tr>
		<%
			for (int i = 0; i < prs.size(); i++) {
				Map mp = (Map) prs.get(i);
		%>
		<tr>
			<td><%=mp.get("name")%></td>
			<td><%=mp.get("lecturer")%></td>
			<td><%=mp.get("period")%></td>
		</tr>
		<%
			}
		%>
	</table>
</body>
</html>
